//
//  PrivateMessages.m
//  Coding_My
//
//  Created by showXxie on 2017/8/23.
//  Copyright © 2017年 sooxie. All rights reserved.
//

#import "PrivateMessages.h"

@implementation PrivateMessages


+(NSDictionary *)mj_objectClassInArray {
    
    return @{
             @"list":@"PrivateMessage"
             };
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _propertyArrayMap = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"PrivateMessage", @"list", nil];
        _canLoadMore = YES;
        _isLoading = _willLoadMore = _isPolling = NO;
        _page = [NSNumber numberWithInteger:1];
        _pageSize = [NSNumber numberWithInteger:20];
        _curFriend = nil;
    }
    return self;
}


- (NSMutableArray *)list{
    if (!_list) {
        _list = [[NSMutableArray alloc] init];
    }
    return _list;
}

- (NSMutableArray *)nextMessages{
    if (!_nextMessages) {
        _nextMessages = [[NSMutableArray alloc] init];
    }
    return _nextMessages;
}

- (NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

- (NSString *)toPath{
    NSString *path;
    if (_curFriend) {
        path = [NSString stringWithFormat:@"api/message/conversations/%@/prev", _curFriend.global_key];
    }else{
        path = @"api/message/conversations";
    }
    return path;
}

- (NSDictionary *)toParams{
    NSDictionary *params = nil;
    if (_curFriend) {
        NSNumber *prevId = kDefaultLastId;
        
        if (_willLoadMore && _list.count > 0) {
            PrivateMessage *prev_Msg = [_list lastObject];
            prevId = prev_Msg.id;
        }
        
        params = @{@"id" : prevId,
                   @"pageSize" : _pageSize};
        
    }else{
        
        params = @{@"page" : _willLoadMore? [NSNumber numberWithInt:_page.intValue +1]: [NSNumber numberWithInt:1],
                   @"pageSize" : _pageSize};
        
    }
    return params;
}


+ (PrivateMessages *)priMsgsWithUser:(User *)user{
    PrivateMessages *priMsgs = [[PrivateMessages alloc] init];
    priMsgs.curFriend = user;
    return priMsgs;
}


- (void)configWithObj:(id)anObj{
    if ([anObj isKindOfClass:[PrivateMessages class]]) {
        PrivateMessages *priMsgs = (PrivateMessages *)anObj;
        self.page = priMsgs.page;
        self.pageSize = priMsgs.pageSize;
        self.totalPage = priMsgs.totalPage;
        if (!_willLoadMore) {
            [self.list removeAllObjects];
        }
        [self.list addObjectsFromArray:priMsgs.list];
        self.canLoadMore = _page.intValue < _totalPage.intValue;
    }else if ([anObj isKindOfClass:[NSArray class]]){
        NSArray *list = (NSArray *)anObj;
       
        if (!_willLoadMore) {
            [self.list removeAllObjects];
        }
      
        [self.list addObjectsFromArray:list];
        self.canLoadMore = list.count > 0;
    }
    [self reset_dataList];
}


- (NSMutableArray *)reset_dataList{
   
    [self.dataList removeAllObjects];
   
    if (_list.count > 0) {
        self.dataList = [_list mutableCopy];
    }
    
    if (_nextMessages.count > 0) {
        [self.dataList insertObjects:_nextMessages atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _nextMessages.count)]];
    }
    return _dataList;
}



@end
